from brian import *
from brian.library.random_processes import *
from brian.library.synapses import  *
import mydataIN as mydatic
import Functions as func


def i_inj(t):
    if t <= 1.1*ms and t > 1 *ms:
        #return 1.345 *nA
        return 0*nA
    else:
        return 0*nA

            

def current_inj(t):
    if t > 0*msecond:
        return -0.5*namp
    else:
        return 0*mamp

class NeuronFactory:
    @magic_return
    def generate(self, size, init='def'):
        mdIN = mydatic.INVars()
        print mdIN.area
        eqsIN=Equations('''
        dvm/dt = (-(mdIN.gna*mna*mna*mna*hna*(vm-mdIN.Ena))-(mdIN.gk*mk*mk*mk*mk*(vm-mdIN.Ek))-mdIN.gl*(vm-mdIN.El)+ge_current)/mdIN.cpf : mvolt
        dmna/dt = (func.m_inf_na(vm/mvolt)-mna)/mdIN.taumna : 1
        dhna/dt = (func.h_inf_na(vm/mvolt)-hna)/func.tau_h_na(vm/mvolt) : 1
        dmk/dt = (func.m_inf_k(vm/mvolt)-mk)/func.tau_m_k(vm/mvolt) : 1
        ''')
        eqsIN+=alpha_conductance(input='ge',E=mdIN.Ee, tau=mdIN.taue,conductance_name='epsp') 
        eqsIN+=OrnsteinUhlenbeck('I',mu=0*nA,sigma=0.002*nA,tau=10*ms)
        NG=NeuronGroup(size,model=eqsIN,
        threshold=EmpiricalThreshold(threshold=-20*mV),
        implicit=True)
        #magic_register(NG)
        return NG
    def initNet(self, NG, init='def'): 
        mdIN = mydatic.INVars()
        vin =mdIN.vinit
        if init != 'def':
            vin=init
        
        NG.vm=vin*mV
        NG.mna = func.m_inf_na(vin)
        NG.hna = func.h_inf_na(vin)
        NG.mk = func.m_inf_k(vin);
        #NG.El = mdIN.El*mV 
        #magic_register(NG)
        return NG